package 树;

import 抽象数据类型.TreeNode;

/**
 * @author: yangwk
 * @date: 2023/4/4
 * @description:
 **/
public class 二叉树直径 {
    public static int diameter = 0;
    public static int DiameterOfTree(TreeNode root) {
        int left, right;
        if (root == null) {
            return 0;
        }
        left = DiameterOfTree(root.left);
        right = DiameterOfTree(root.right);
        if (left + right > diameter) {
            diameter = left + right;
        }
        return Math.max(left, right) + 1;
    }    //后序遍历
}
